<?php

namespace MyApp\Controllers\Admin;

use limx\func\Image;
use MyApp\Models\Student;

class IndexController extends ControllerBase
{
    public function indexAction($type = 0)
    {
        $student = Student::find();
        $this->view->student = $student;
        $this->view->type = $type == 0 ? 1 : 0;
        return $this->view->render('admin/index', 'index');
    }

    public function addAction($id)
    {
        $student = Student::findFirst($id);
        $this->view->student = $student;
        return $this->view->render('admin/index', 'add');
    }

    public function pfnAddAction()
    {
        $data = $this->request->get();
        $user = new Student();
        if ($user->save($data)) {
            $id = $user->id;
            qrcode($id, $user->name);
            return success([$id]);
        }
        return error("保存失败！");
    }

    public function uploadAction()
    {
        $pic = $this->request->get('data');
        $today = Date("Ymd");
        $root = "uploads/" . $today . "/";
        $res = Image::base64ToImg($root, $pic);
        $config = di('config');
        if ($res) {
            //生成缩略图
            Image::compress($root . $res, 450, 450);
            $rs['data'] = $config->application->baseUri . $root . $res;
            $rs['x_data'] = $config->application->baseUri . $root . str_replace('.', '_x.', $res);

            return success($rs);
        }
        return error("头像上传失败！");
    }
}
